package com.tanhua.dubbo.server.api;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.convert.Convert;
import com.alibaba.dubbo.config.annotation.Service;
import com.tanhua.dubbo.server.pojo.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.redis.core.RedisTemplate;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * @author Ling
 * @Title: null.java
 * @Package com.tanhua.dubbo.server.api
 * @Description:
 * @date 2021/9/14 13:58
 */
@Service(version = "1.0.0")
public class TestSoulApiImpl implements TestSoulApi {
    @Autowired
    private MongoTemplate mongoTemplate;

    @Autowired
    private RedisTemplate<String, String> redisTemplate;

    public void saveOption(Object o) {
        mongoTemplate.save(o);
    }

    /**
     * 根据分数查询结果文档
     *
     * @param score
     * @return
     */
    public TestSoulResult queryTestSoulResultByScore(Integer score) {
        Integer integer = queryResultIdByScore(score);
        Query query = Query.query(Criteria.where("resultId").is(integer));
        List<TestSoulResult> testSoulResults = mongoTemplate.find(query, TestSoulResult.class);
        return CollUtil.isEmpty(testSoulResults) ? null : testSoulResults.get(0);
    }

    public Set<Long> queryUserIdsByScore(Integer score) {
//        Integer integer = queryResultIdByScore(score);
        Set<Long> set = new HashSet<>();

        Query query = Query.query(Criteria.where("ScoreQ1").gte(score - 5).and("ScoreQ1").lte(score + 5));
        List<TestSoulResult> testSoulResults = mongoTemplate.find(query, TestSoulResult.class);
        List<Object> userIds1 = CollUtil.getFieldValues(testSoulResults, "userId");

        Query query2 = Query.query(Criteria.where("ScoreQ2").gte(score - 5).and("ScoreQ1").lte(score + 5));
        List<TestSoulResult> testSoulResults2 = mongoTemplate.find(query, TestSoulResult.class);
        List<Object> userIds2 = CollUtil.getFieldValues(testSoulResults, "userId");

        Query query3 = Query.query(Criteria.where("ScoreQ3").gte(score - 5).and("ScoreQ1").lte(score + 5));
        List<TestSoulResult> testSoulResults3 = mongoTemplate.find(query, TestSoulResult.class);
        List<Object> userIds3 = CollUtil.getFieldValues(testSoulResults, "userId");

        if (CollUtil.isEmpty(userIds1)) {
            for (Object o : userIds1) {
                set.add(Convert.toLong(o));
            }
        }
        if (CollUtil.isEmpty(userIds2)) {
            for (Object o : userIds2) {
                set.add(Convert.toLong(o));
            }
        }
        if (CollUtil.isEmpty(userIds3)) {
            for (Object o : userIds3) {
                set.add(Convert.toLong(o));
            }
        }

        return set;
    }


    public TestSoulResult queryTestSoulResultById(Integer id) {
//        Integer integer = queryResultIdByScore(score);
        Query query = Query.query(Criteria.where("resultId").is(id));
        List<TestSoulResult> testSoulResults = mongoTemplate.find(query, TestSoulResult.class);
        return CollUtil.isEmpty(testSoulResults) ? null : testSoulResults.get(0);
    }

    /**
     * 根据userId 查询 问卷的得分
     *
     * @param userId
     * @return
     */
    public TestSoulRelations queryTestSoulRelationsByUserID(Long userId) {
        Query query = Query.query(Criteria.where("userId").is(userId));
        List<TestSoulRelations> testSoulRelations = mongoTemplate.find(query, TestSoulRelations.class);
        return CollUtil.isEmpty(testSoulRelations) ? null : testSoulRelations.get(0);
    }

    /**
     * 根据选项Id的集合 查询所有的选项
     *
     * @param ids
     * @return
     */
    public List<TestSoulOption> queryTestSoulOptionByIds(List<String> ids) {
        Query query = Query.query(Criteria.where("optionId").in(ids));
        List<TestSoulOption> testSoulOptions = mongoTemplate.find(query, TestSoulOption.class);
        return CollUtil.isEmpty(testSoulOptions) ? null : testSoulOptions;
    }

    /**
     * 根据问题Id的集合 查询所有的问题
     *
     * @param ids
     * @return
     */
    public List<TestSoulQuestions> queryTestSoulQuestionsByIds(List<Integer> ids) {
        Query query = Query.query(Criteria.where("questionId").in(ids));
        List<TestSoulQuestions> testSoulQuestions = mongoTemplate.find(query, TestSoulQuestions.class);
        return CollUtil.isEmpty(testSoulQuestions) ? null : testSoulQuestions;
    }

    /**
     * 根据问卷id 查找问卷信息
     *
     * @param id
     * @return
     */
    public List<TestSoulQuestionnaire> queryTestSoulQuestionnaireById(Integer id) {
        Query query = Query.query(Criteria.where("questionnaireId").is(id));
        List<TestSoulQuestionnaire> testSoulQuestionnaires = mongoTemplate.find(query, TestSoulQuestionnaire.class);
        return CollUtil.isEmpty(testSoulQuestionnaires) ? null : testSoulQuestionnaires;
    }

    /**
     * 获取所有的问卷
     *
     * @param
     * @return
     */
    public List<TestSoulQuestionnaire> queryTestSoulQuestionnaireAll() {
        List<TestSoulQuestionnaire> testSoulQuestionnaires = mongoTemplate.findAll(TestSoulQuestionnaire.class);
        return CollUtil.isEmpty(testSoulQuestionnaires) ? null : testSoulQuestionnaires;
    }

    /**
     * 根据得分 返回不同的报告id
     *
     * @param score
     * @return
     */
    private Integer queryResultIdByScore(Integer score) {
        if (score < 21) {
            return 1;
        } else if (score < 40) {
            return 2;
        } else if (score < 56) {
            return 3;
        } else {
            return 4;
        }
    }

    public void saveScore(Long userId, Integer score) {
        Query query = Query.query(Criteria.where("userId").is(userId));
        List<TestSoulRelations> testSoulRelations = mongoTemplate.find(query, TestSoulRelations.class);
        if (CollUtil.isEmpty(testSoulRelations)) {
            TestSoulRelations newRelation = new TestSoulRelations();
            newRelation.setUserId(userId);
            newRelation.setScoreQ1(score);
            newRelation.setScoreQ2(score);
            newRelation.setScoreQ3(score);
            mongoTemplate.save(newRelation);
        }
        TestSoulRelations testSoulRelations1 = testSoulRelations.get(0);
        testSoulRelations1.setScoreQ1(score);
        testSoulRelations1.setScoreQ2(score);
        testSoulRelations1.setScoreQ3(score);
        mongoTemplate.save(testSoulRelations1);
    }


}
